using AutoMapper;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ShopPowerTemplate.Entities;
using ShopPowerTemplate.IoC;
using ShopPowerTemplate.Presentation.Mvc.Controllers;
using ShopPowerTemplate.Presentation.Mvc.Extensions.BootStrapper;
using ShopPowerTemplate.Presentation.Mvc.ViewModels;
using ShopPowerTemplate.Services.Tests;
using System.Web.Mvc;

namespace ShopPowerTemplate.Presentation.Mvc.Test
{
    [TestClass]
    public class ReviewControllerTestBase
    {
        [TestInitialize]
        public virtual void TestInitialize()
        {
            IBootStrapper bootStrapper = new DefaultBootStrapper(IoCFactory.Instance.CurrentContainer);
            bootStrapper.Boot();
        }

        [TestMethod]
        public virtual void ReviewController_IndexTest()
        {
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<ReviewController>());

            var result = controller.Index() as ViewResult;
            
            Assert.IsNotNull(result);
            //ViewName is empty when ViewResult matches action name
            Assert.IsTrue(string.IsNullOrEmpty(result.ViewName));
            Assert.IsTrue(result.Model is IndexReviewViewModel);
        }

        [TestMethod]
        public virtual void ReviewController_LoadTest()
        {
            ReviewServiceUtil.CreateReview();
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<ReviewController>());

            var result = controller.Load("", "", 0, 10, "") as JsonResult;

            var rows = ModelHelper.GetList(result.Data);
            Assert.IsTrue(rows.Count > 0);
        }

        [TestMethod]
        public virtual void ReviewController_DeleteTest()
        {
            Review review = ReviewServiceUtil.CreateReview();
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<ReviewController>());

            var result = controller.Delete(review.Id) as ContentResult;

            Assert.IsNotNull(result);
            Assert.IsTrue(string.IsNullOrEmpty(result.Content));
        }

        [TestMethod]
        public virtual void ReviewController_ReportTest()
        {
            Review review = ReviewServiceUtil.CreateReview();
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<ReviewController>());

            var result = controller.Report() as FileContentResult;

            Assert.IsNotNull(result);
            Assert.IsTrue(result.ContentType == "text/csv");
            Assert.IsNotNull(result.FileContents.Length > 0);
        }

        [TestMethod]
        public virtual void ReviewController_GetCreateTest()
        {
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<ReviewController>());

            var result = controller.Create() as ViewResult;

            Assert.IsNotNull(result);
            //ViewName is empty when ViewResult matches action name
            Assert.IsTrue(string.IsNullOrEmpty(result.ViewName));
            Assert.IsTrue(result.Model is CreateReviewViewModel);
        }

        [TestMethod]
        public virtual void ReviewController_PostCreateTest()
        {
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<ReviewController>());
            Review review = ReviewServiceUtil.GetReview();
            CreateReviewViewModel viewModel = Mapper.Map<Review, CreateReviewViewModel>(review);

            var result = controller.Create(viewModel) as RedirectToRouteResult;

            Assert.IsNotNull(result);
            Assert.AreEqual(result.RouteValues["action"], "Edit"); //Create redirect to edit action on success
            Assert.IsTrue((int)result.RouteValues["id"] > 0); //Check new item id
        }

        [TestMethod]
        public virtual void ReviewController_GetEditTest()
        {
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<ReviewController>());
            Review review = ReviewServiceUtil.CreateReview();

            var result = controller.Edit(review.Id) as ViewResult;

            Assert.IsNotNull(result);
            //ViewName is empty when ViewResult matches action name
            Assert.IsTrue(string.IsNullOrEmpty(result.ViewName));
            Assert.IsTrue(result.Model is EditReviewViewModel);
            Assert.AreEqual((result.Model as EditReviewViewModel).Comment, review.Comment);
        }

        [TestMethod]
        public virtual void ReviewController_PostEditTest()
        {
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<ReviewController>());
            Review review = ReviewServiceUtil.CreateReview();
            review.Comment = "Test";

            EditReviewViewModel viewModel = Mapper.Map<Review, EditReviewViewModel>(review);
            controller.Edit(viewModel);

            var result = controller.Edit(review.Id) as ViewResult;
            Assert.IsNotNull(result);
            Assert.AreEqual((result.Model as EditReviewViewModel).Comment, "Test");
        }

        [TestMethod]
        public virtual void ReviewController_DetailTest()
        {
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<ReviewController>());
            Review review = ReviewServiceUtil.CreateReview();

            var result = controller.Details(review.Id) as ViewResult;

            Assert.IsNotNull(result);
            //ViewName is empty when ViewResult matches action name
            Assert.IsTrue(string.IsNullOrEmpty(result.ViewName));
            Assert.IsTrue(result.Model is DetailsReviewViewModel);
            Assert.AreEqual((result.Model as DetailsReviewViewModel).Comment, review.Comment);
        }

    }
}

